import {ref} from "vue";
import {defineStore} from "pinia";
import {reactive} from "vue";

const piniaPluginConfig = {persist: {storage: sessionStorage}}

export const useLoginStore = defineStore('userLoginStore', () => {
   const loginSetting = reactive({
       loginDialog:false,
       loginLoading:false,
       beforeLoginRoutePath:null
   })
    const userInfo = reactive({
        isLogin:false,
        userId:null,
        avatar:null,
        nickName:null,
        email:null
    })
    const setLoginUserInfo = (user)=>{

       userInfo.isLogin = true
       userInfo.userId = user.userId
       userInfo.avatar = user.avatar
       userInfo.nickName = user.nickName
       userInfo.email = user.email
    }
    const userLogout = ()=>{
        userInfo.isLogin =false
        userInfo.userId = null
        userInfo.avatar = null
        userInfo.nickname = null
        userInfo.email = null
        sessionStorage.removeItem('accessToken')
        sessionStorage.removeItem('refreshToken')
    }
    const userIsLogin = ()=>{
       return userInfo.isLogin
    }
    const getUserLoginInfo = ()=>{
       return userInfo
    }
    const getLoginSetting = ()=>{
       return loginSetting
    }
    const openLoginDialog = ()=>{
        loginSetting.loginDialog = true
    }
    const closeLoginDialog = ()=>{
       loginSetting.loginDialog = false
    }
    const openLoginLoading = ()=>{
        loginSetting.loginLoading = true
    }
    const closeLoginLoading = ()=>{
        loginSetting.loginLoading = false
    }

    const setBeforeLoginRoutePath = (path)=>{
       loginSetting.beforeLoginRoutePath = path
    }
    const getBeforeLoginRoutePath = ()=>{
        return loginSetting.beforeLoginRoutePath
    }
    return {
        userInfo,
       userIsLogin,setLoginUserInfo,userLogout,getUserLoginInfo,getLoginSetting,closeLoginDialog,closeLoginLoading,
        openLoginDialog,openLoginLoading,setBeforeLoginRoutePath,getBeforeLoginRoutePath
    }
},piniaPluginConfig)

export const useSystemSettingStore = defineStore('setting',()=>{
    const setting = reactive({
        currentSideBarActive:'index'
    })
    const getCurrentSideBarActive = ()=>{
        return setting.currentSideBarActive
    }
    const setCurrentSideBarActive = (active)=>{
        setting.currentSideBarActive = active
    }
    return {getCurrentSideBarActive,setCurrentSideBarActive,setting}
},piniaPluginConfig)